<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="门店id" prop="storeId">-->
<!--        <el-input-->
<!--          v-model="queryParams.storeId"-->
<!--          placeholder="请输入门店id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="申请人id" prop="applicantId">-->
<!--        <el-input-->
<!--          v-model="queryParams.applicantId"-->
<!--          placeholder="请输入申请人id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="提现金额" prop="withdrawalAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.withdrawalAmount"-->
<!--          placeholder="请输入提现金额"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable size="small">
          <el-option
            v-for="dict in auditStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="审核人" prop="auditor">-->
<!--        <el-input-->
<!--          v-model="queryParams.auditor"-->
<!--          placeholder="请输入审核人"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.auditTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="支付订单号"  label-width="100px" prop="paymentOrder">
        <el-input
          v-model="queryParams.paymentOrder"
          placeholder="请输入支付订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="paymentStatus">
        <el-select v-model="queryParams.paymentStatus" placeholder="请选择支付状态" clearable size="small">
          <el-option
            v-for="dict in paymentStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="支付失败原因" prop="paymentFailure">-->
<!--        <el-input-->
<!--          v-model="queryParams.paymentFailure"-->
<!--          placeholder="请输入支付失败原因"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="支付成功原因" prop="paymentSuccessful">-->
<!--        <el-input-->
<!--          v-model="queryParams.paymentSuccessful"-->
<!--          placeholder="请输入支付成功原因"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon=""-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['shopping:withdraw:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain

          size="mini"
          :disabled="multiple"
          @click="passAll"
          v-hasPermi="['shopping:withdraw:edit']"
        >批量通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain

          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shopping:withdraw:remove']"
          v-if="false"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain

          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['shopping:withdraw:export']"
          v-if="false"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :height="tableHeight" :data="withdrawList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" show-overflow-tooltip />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
<!--      <el-table-column label="门店id" align="center" prop="storeId" />-->
<!--      <el-table-column label="申请人id" align="center" prop="applicantId" />-->
      <el-table-column label="提现金额" align="center" prop="withdrawalAmount" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="auditStatusOptions" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditor" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付订单号" align="center" prop="paymentOrder" />
      <el-table-column label="支付状态" align="center" prop="paymentStatus">
        <template slot-scope="scope">
          <dict-tag :options="paymentStatusOptions" :value="scope.row.paymentStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="支付失败原因" align="center" prop="paymentFailure" :show-overflow-tooltip="true"/>
      <el-table-column label="支付成功原因" align="center" prop="paymentSuccessful" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
     <!--     <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shopping:withdraw:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shopping:withdraw:remove']"
          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shopping:withdraw:edit']"
            v-if="scope.row.auditStatus == 'audit_IN'"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shopping:withdraw:edit']"
            v-else
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改佣金提现审核对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
<!--        <el-form-item label="门店id" prop="storeId">-->
<!--          <el-input v-model="form.storeId" placeholder="请输入门店id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="申请人id" prop="applicantId">-->
<!--          <el-input v-model="form.applicantId" placeholder="请输入申请人id" />-->
<!--        </el-form-item>-->
        <el-form-item label="提现金额" prop="withdrawalAmount">
          <el-input v-model="form.withdrawalAmount" placeholder="请输入提现金额" readonly/>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态"  disabled readonly>
            <el-option
              v-for="dict in auditStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核人" prop="auditor">
          <el-input v-model="form.auditor" placeholder="请输入审核人" readonly/>
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime" >
          <el-date-picker clearable size="small"
            v-model="form.auditTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付订单号"  prop="paymentOrder" >
          <el-input v-model="form.paymentOrder" placeholder="请输入支付订单号" readonly/>
        </el-form-item>
        <el-form-item label="支付状态" prop="paymentStatus">
          <el-select v-model="form.paymentStatus" placeholder="请选择支付状态"  disabled readonly>
            <el-option
              v-for="dict in paymentStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付失败原因"  prop="paymentFailure">
          <el-input v-model="form.paymentFailure" placeholder="请输入支付失败原因" readonly/>
        </el-form-item>
        <el-form-item label="支付成功原因" prop="paymentSuccessful">
          <el-input v-model="form.paymentSuccessful" placeholder="请输入支付成功原因" readonly/>
        </el-form-item>
        <!--<el-form-item label="创建来源" prop="createType">
          <el-select v-model="form.createType" placeholder="请选择创建来源">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="更新来源" prop="updateType">
          <el-select v-model="form.updateType" placeholder="请选择更新来源">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('pass')"  v-if="form.auditStatus == 'audit_IN'">通 过</el-button>
        <el-button type="warning" @click="showRefuse" v-if="form.auditStatus == 'audit_IN'">拒 绝</el-button>
        <el-button @click="cancel" >取 消</el-button>
      </div>
    </el-dialog>

    <!-- 拒绝信息对话框 -->
    <el-dialog :close-on-click-modal="false"  title="拒绝原因" :visible.sync="refuseOpen" width="500px" append-to-body>
      <el-form ref="refuseForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="拒绝原因" prop="refuseMsg">
          <el-input v-model="form.refuseMsg" type="textarea" placeholder="请输入拒绝原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" @click="submitForm('refuse')">确定</el-button>
        <el-button @click="refuseCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWithdraw, getWithdraw, logicDelWithdraw, addWithdraw, updateWithdraw,refuse,pass, exportWithdraw } from "@/api/shopping/withdraw";

export default {
  name: "Withdraw",
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 佣金提现审核表格数据
      withdrawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态字典
      auditStatusOptions: [],
      // 支付状态字典
      paymentStatusOptions: [],
      // 拒绝原因对话框
      refuseOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId: null,
        applicantId: null,
        withdrawalAmount: null,
        auditStatus: null,
        auditor: null,
        auditTime: null,
        paymentOrder: null,
        paymentStatus: null,
        paymentFailure: null,
        paymentSuccessful: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        refuseMsg: [
          { required: true, message: "拒绝原因不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.calHeight()
      this.getDicts("jy_audit_state").then(response => {
      this.auditStatusOptions = response.data;
    });
    this.getDicts("huifu_pay_state").then(response => {
      this.paymentStatusOptions = response.data;
    });
  },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询佣金提现审核列表 */
    getList() {
      this.loading = true;
      listWithdraw(this.queryParams).then(response => {
        this.withdrawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //拒绝按钮
    showRefuse(){
      this.refuseOpen = true;
    },
    refuseCancel(){
      this.refuseOpen = false;
    },
    //批量通过按钮
    passAll(){
      const id = this.ids;
      let arr = this.withdrawList;
      for(let item of arr){
        for(let arrId of id){
          if(arrId+'' == item.id+''){
            if(item.auditStatus == 'audit_PASS'){
              this.msgError("已通过审核的无法再次审核");
              return
            }
          }
        }
      }
      pass(id).then(response => {
        this.msgSuccess("成功");
        this.getList();
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeId: null,
        applicantId: null,
        withdrawalAmount: null,
        auditStatus: "0",
        auditor: null,
        auditTime: null,
        paymentOrder: null,
        paymentStatus: "0",
        paymentFailure: null,
        paymentSuccessful: null,
        delState: null,
        delUser: null,
        delType: null,
        delTime: null,
        createBy: null,
        createType: null,
        createTime: null,
        updateBy: null,
        updateType: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加佣金提现审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWithdraw(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改佣金提现审核";
      });
    },
    /** 提交按钮 */
    submitForm(type) {
      const id = this.form.id || this.ids;
      let bool = true;
      if (type == "refuse") {
        this.$refs["refuseForm"].validate(valid => {
          bool = valid;
        });
      }
      if(bool) {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (type == 'refuse') {
              refuse(this.form).then(response => {
                this.msgSuccess("成功");
                this.open = false;
                this.refuseOpen = false;
                this.getList();
              });
            } else {
              pass(id).then(response => {
                this.msgSuccess("成功");
                this.open = false;
                this.refuseOpen = false;
                this.getList();
              });
            }
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelWithdraw(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有佣金提现审核数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportWithdraw(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
